package com.hns.can.mainte.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.mainte.entity.CommMftCopaEntity;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;

/**
 * 报表-机务报表-车辆体检-维修手册
 * @author:yecb
 * @email:lps00@163.com
 * @version Revision 2.0.0
 */
@Service
public class ReportServiceManualService extends CommonService {
    @Autowired
    private FillCodeDscValue codeDscValue;

   /**
    * 新增故障类型对照
    * @param entity
    * @author:yecb
 * @throws Exception 
    * @email:lps00@163.com
    */
    @Transactional
    public void addCommMftCopaEntity(CommMftCopaEntity entity) throws Exception {
        commonEntityDao.save(entity);
    }
   /**
    * 修改故障类型对照
    * @param entity
    * @author:yecb
    * @throws Exception 
    * @email:lps00@163.com
    */
    @Transactional
    public void uptCommMftCopaEntity(CommMftCopaEntity entity,SysUserEntity sysUserEntity) throws Exception {
//    	if(PubUtil.isNotEmpty(entity.getMftAnl())||PubUtil.isNotEmpty(entity.getSolutionPlan())){
	    	if(PubUtil.isNotEmpty(entity.getMftInsideCode())){
	    		int count = (Integer)this.sqlMapDao.selectOne("reportServiceManual.checkMftInsideCode", entity);
	    		if(count > 0){
	    			throw new BusinessException(MsgType.ERROR.getFlag(), "故障闪码重复！");
	    		}
	    	}
    		sqlMapDao.update("reportServiceManual.uptMftInfo", entity);
//    	}
    }
    
  /**
   * 根据故障类型代码查找故障类型对照
   * @param mftTypeCode  故障类型代码
   * @return
   * @author:yecb
   * @email:lps00@163.com
   */
    @Transactional
    public CommMftCopaEntity getCommMftCopaById(String mftTypeCode){
        CommMftCopaEntity entity = commonEntityDao.find(mftTypeCode, CommMftCopaEntity.class);
        return entity;
    }
    
    /**
     * 根据故障类型名称查找故障类型对照
     * @param mftTypeName  故障类型代码
     * @return
     * @author:yecb
     * @email:lps00@163.com
     */
      @Transactional
      public CommMftCopaEntity getCommMftCopaByName(String mftTypeName){
          CommMftCopaEntity entity = commonEntityDao.find(mftTypeName, CommMftCopaEntity.class);
          return entity;
      }
    
   /**
    * 删除故障类型对照
    * @param entity
    * @author:yecb
    * @email:lps00@163.com
    */
    @Transactional
    public void delCommMftCopaEntity(CommMftCopaEntity entity) {
        CommMftCopaEntity delEntity = commonEntityDao.find(entity.getMftTypeCode(), CommMftCopaEntity.class);
        commonEntityDao.delete(delEntity);
    }
    /**
     * 查找故障列表信息
     * @param entity
     * @param sysUserEntity
     * @return
     * @author:yecb
     * @email:lps00@163.com
     */
    @SuppressWarnings("unchecked")
	@Transactional
    public Pagination getMftInfoList(CommMftCopaEntity entity ,SysUserEntity sysUserEntity) throws Exception {
        //若参数没有企业ID，用户的企业ID替代
        if(!PubUtil.isNotEmpty(entity.getCorpId())){
            entity.setCorpId(sysUserEntity.getCorpId());
        }
        
        Pagination page = this.myBatisPage("reportServiceManual.getMftInfoCnt", "reportServiceManual.getMftInfoList", entity, CommMftCopaEntity.class);
        List list = page.getData();
//     	List<CommMftCopaEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("reportServiceManual.getMftInfoList", entity), CommMftCopaEntity.class);
        Map<String,String> map = new HashMap<String,String>();
        map.put("corpName",  GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo = new FillParamsVo("corpId", map);
        codeDscValue.fillInfoValue(list,new String[]{GlobalConst.CACHE_KEY_SYS_CORP },vo);
        List tmp = new ArrayList<CommMftCopaEntity>();
        if(PubUtil.isNotEmpty(list)){
//            for(CommMftCopaEntity item:list){
        	for(int i=0;i<list.size();i++){
        		CommMftCopaEntity item = (CommMftCopaEntity)list.get(i);
                if(!item.getCorpId().equals("39708c61759b48748c5bd853ba64ded5")){
                    tmp.add(item);
                }
            }
        }
//        return tmp;
        page.setData(tmp);
        return page;
    }
    
    /**
     * 查找到当前登录公司的ECU节点列表
     * @param sysUserEntity
     * @author:yecb
     * @email:lps00@163.com
     */
     @SuppressWarnings("unchecked")
	@Transactional
     public List<CommMftCopaEntity> getEcuTypeInfoList(SysUserEntity sysUserEntity) {
    	CommMftCopaEntity conditionVo=new CommMftCopaEntity();
     	conditionVo.setCorpId(sysUserEntity.getCorpId());
     	List<CommMftCopaEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("reportServiceManual.getEcuTypeInfoList", conditionVo), CommMftCopaEntity.class);
     	return list;
     }
    
}
